package cn.anosi.service.auth.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import cn.anosi.entity.auth.Permission;
import cn.anosi.entity.auth.Role;
import cn.anosi.entity.auth.User;
import cn.anosi.entity.auth.UserRole;
import cn.anosi.page.PageResponseBean;
import cn.anosi.page.Pager;
import cn.anosi.service.auth.UserService;
import cn.anosi.service.base.impl.BaseServiceImpl;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

	@Override
	public User save(User entity) {
		return userRepository.save(entity);
	}

	@Override
	public int delete(Serializable id) {
		return 0;
	}

	@Override
	public User getById(Serializable id) {
		return userRepository.getById(id);
	}

	public List<User> findAll() {
		return userRepository.findAll();
	}

	@Override
	public User login(String username, String password) {
		return userRepository.findByUsernameAndPassword(username, password);
	}

	/**
	 * 返回带分页的用户列表
	 */
	@Override
	public PageResponseBean<User> findAll(Pager pager) {
		PageResponseBean<User> pageResponseBean = new PageResponseBean<>();
		Page<User> page = userRepository.findAll(new PageRequest(pager.getPageNum(), pager.getPageSize(), new Sort(pager.getDirection() , pager.getProperty())));

		List<User> data = page.getContent();
		
		pageResponseBean.setData(data);
		pageResponseBean.setDraw(pager.getDraw() + 1);
		pageResponseBean.setRecordsTotal(page.getTotalElements());
		pageResponseBean.setRecordsFiltered(page.getTotalElements());
		return pageResponseBean;
	}

	@Override
	public int lock(Integer id, String locked) {
		return userRepository.lock(id, locked);
	}

	@Override
	public PageResponseBean<User> findAll(Pager pager, String name) {
		List<User> users  = userRepository.users(name, new PageRequest(pager.getPageNum(), pager.getPageSize()));
		PageResponseBean<User> pageResponseBean = new PageResponseBean<>();
		pageResponseBean.setData(users);
		pageResponseBean.setDraw(pager.getDraw() + 1);
		pageResponseBean.setRecordsTotal(userRepository.count());
		pageResponseBean.setRecordsFiltered(userRepository.count());
		return pageResponseBean;
	}
	
	@Override
	public int batchDel(List<Integer> ids) {
		return userRepository.batchDel(ids);
	}

	@Override
	public UserRole saveUser(User user, Integer roleId) {
		User u = userRepository.save(user);
		Role role = new Role();
		role.setId(roleId);
		UserRole userRole = new UserRole();
		userRole.setUser(u);
		userRole.setRole(role);
		return userRoleRepository.save(userRole);
	}

	@Override
	public List<Permission> menus(Integer id) {
		return userRepository.menus(id);
	}
	
	@Override
	public List<Permission> permissions(Integer id) {
		return userRepository.permissions(id);
	}
	
	@Override
	public User findByUsername(String username) {
		return userRepository.findByUsernameAndLocked(username, "0");
	}

	@Override
	public List<String> findUserRoleNames(Integer id) {
		return userRepository.findUserRoleNames(id);
	}

	@Override
	public List<Integer> roleIds(Integer id) {
		return userRoleRepository.roleIds(id);
	}

	@Override
	public int associateRole(Integer userId, List<Integer> ids) {
		int result = userRoleRepository.deleteRoleByUserId(userId);
		
		int times = 0;
		if(ids.size() >0){
			for (Integer id : ids) {
				times += userRoleRepository.associate(userId ,id);
			}
			customRealm.clearCached();
			return times;
		}else{
			customRealm.clearCached();
			return result;
		}
	}
	
	@Override
	public int update(Integer id, byte[] img) {
		return userRepository.update(id, img);
	}

	@Override
	public List<User> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResponseBean<User> list(Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}

}
